//
//  VehicleMaintenanceViewController.m
//  阿帕
//
//  Created 阿帕 on 2020/1/4.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "VehicleMaintenanceViewController.h"
#import "VehicleMaintenanceCell.h"
#import "VehicleMaintenanceDetailViewController.h"
#import "ReportDetailViewController.h"
#import "MaintenanceApplyViewController.h"

@interface VehicleMaintenanceViewController ()<UITableViewDelegate,UITableViewDataSource,VehicleMaintenanceCellButtonDelegate>

@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (strong , nonatomic)UIButton *addCardButton;

@property (nonatomic, assign) NSInteger pageNum;

@end

static NSString *const cellID = @"VehicleMaintenaceCellID";

static const CGFloat cellHeight = 155;

@implementation VehicleMaintenanceViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self vehicleMaintenaceListRequest];

}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _pageNum = 1;
    
    [self setupBaseView];
    
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"车辆维修";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.addCardButton];
    
}

- (void)vehicleMaintenaceListRequest {
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": DEFULT_LOAD_NUM,
        @"status": _orderStatus,
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], VEHICLEREPAIRAPPLY) withParameter:params withVC:self netName:@"车辆维修列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {

            [weakSelf.dataArray addObjectsFromArray:[VehicleMaintenanceRecordModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];

            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;

        }else{
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }

            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];

        }

        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self vehicleMaintenaceListRequest];
}
- (void)loadMoreData{
    _pageNum ++;
    [self vehicleMaintenaceListRequest];
}

//维修申请
- (void)btnEventClick {
    
    MaintenanceApplyViewController *mavc = [[MaintenanceApplyViewController alloc] init];
    [self.navigationController pushViewController:mavc animated:YES];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VehicleMaintenanceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataArray[indexPath.row];
    cell.delegate = self;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
}

#pragma mark - VehicleMaintenanceCellButtonDelegate
- (void)cellButtonIndexClick:(NSInteger)index withButton:(UIButton *)button {
    
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[self.tableView convertPoint:button.center fromView:button.superview]];
    //    NSLog(@"点击了第%ld个item的第%ld个按钮",(long)indexpath.row, (index-99));
    
    VehicleMaintenanceRecordModel *model = self.dataArray[indexPath.row];
    
    if ([button.titleLabel.text isEqual:@"详情"]) {
        
        VehicleMaintenanceDetailViewController *vmdvc = [[VehicleMaintenanceDetailViewController alloc] init];
        vmdvc.code = model.code;
        [self.navigationController pushViewController:vmdvc animated:YES];
        
    } else {
        //详情
        ReportDetailViewController *rdvc = [[ReportDetailViewController alloc] init];
        rdvc.code = model.code;
        [self.navigationController pushViewController:rdvc animated:YES];
    }
}

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - MenuBarHeight - 100 - 30);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([VehicleMaintenanceCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.view.center;
        _foundImageView.cy_y -=80;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}

- (UIButton *)addCardButton {
    if (!_addCardButton) {
        _addCardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addCardButton setTitle:@"维修申请" forState:UIControlStateNormal];
        _addCardButton.titleLabel.font = font(16);
        cut(_addCardButton, BTN_BORDER_RADIUS);
        [_addCardButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_addCardButton setBackgroundColor:Theme_ColorValue];
        [_addCardButton addTarget:self action:@selector(btnEventClick) forControlEvents:UIControlEventTouchUpInside];
        _addCardButton.frame = CGRectMake(20, ScreenH - NavHeight - MenuBarHeight - 90 - 20, ScreenW-40, 45);
    }
    return _addCardButton;
}
@end
